﻿using System;
    using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Forms;
using ActionServices;
using DevExpress.XtraEditors.Controls;
using System.Collections;

namespace SchoolManagementSystem.Controls
{
    public partial class ComboboxUC : UserControl
    {
        public ComboboxUC()
        {
            InitializeComponent();
            combobox.EditValueChanged += new EventHandler(lookUp_EditValueChanged);
        }

          void lookUp_EditValueChanged(object sender, EventArgs e)
        {
            this.OnTextChanged(e);
        }


          long _iD = -1;
          string _selectedValue;
          frmShowDetailForm _detailFrm;

          DetailUC _uc;

         string _columnDisplay; //column1,column2,..
        public string ColumnDisplay
         {
             get { return _columnDisplay; }
             set
             {
                 _columnDisplay = value;
                 BindingAllData();
             }
        }


          public string ColumnMain;

          public DetailUC Uc
          {
              get { return _uc; }
              set
              {
                  _uc = value;
                  _detailFrm = new frmShowDetailForm(_uc);
              }
          }

          public long ID
          {
              get
              {
                  try
                  {
                      _iD = (long)combobox.EditValue;
                  }
                  catch (Exception)
                  {

                  }

                  return _iD;
              }
              set
              {
                  //combobox.EditValue = 0;
                  if (_uc != null)
                  {
                      _iD = value;
                      _uc.ID = _iD;
                      BindingAllData();
                      combobox.EditValue = _iD;
                  }

                 

                  combobox.EditValue = value;
              }
          }

        //get with guid id type
          public string SelectedValue
          {
              get {
                  try
                  {
                      return this.combobox.GetColumnValue(_model.GetType().Name.Replace("ActionService", "") + "GuidID").ToString();
                  }
                  catch
                  {
                      return null;
                  }
              }
              set
              {
                  try
                  {
                      _selectedValue = value;
                      string guid = _model.GetType().Name + "GuidID";
                      var objects = ((IEnumerable)this.combobox.Properties.DataSource).Cast<object>().ToList();
                      var obj = objects.FirstOrDefault(c => c.GetType().GetProperty(guid).GetValue(objects, null).Equals(_selectedValue));
                      if (obj != null)
                      {
                          if (_uc != null)
                          {
                              _iD = (long)obj.GetType().GetProperty("ID").GetValue(obj, null);
                              _uc.ID = _iD;
                              combobox.EditValue = _iD;
                          }
                      }
                      else
                      {
                          combobox.EditValue = -1;
                      }
                  }
                  catch { combobox.EditValue = -1; }
              }
          }
            

          private void combobox_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
          {
              if (e.Button.Index == 1) //detail
              {
                  try
                  {
                      if (  _uc.ID !=-1)
                      {
                          if(_detailFrm.UC==null)
                          {
                              _detailFrm = new frmShowDetailForm(_uc);
                            }
                          _uc.ID = (long)combobox.EditValue;
                          _detailFrm.ShowDock(null, null);
                      }
                      else
                      {
                          MessageControl.ShowMessage("Bạn chưa chọn dữ liệu","Thông báo", MessageControl.Icon.Warning);
                      }
                  }
                  catch (Exception)
                  {

                      _uc.ID = -1;
                      _detailFrm.ShowDock(null, null);
                  }

                  //_uc.Display();
              }
              else if (e.Button.Index == 2)    //Add
              {
                  _uc.ID = -1;
                  if (_detailFrm.UC == null)
                  {
                      _detailFrm = new frmShowDetailForm(_uc);
                  }
                  _detailFrm.ShowDock(null, null);
              }
              else if (e.Button.Index == 3)//reload
              {
                  IsBind = false;
              }
              else if (e.Button.Index == 4)//emty
              {
                  ID = -1;
              }
             
              if (_uc != null)
                  _uc.ID = ID;

          

              if (e.Button.Index == 0)
              {
                  
                    //  BindingAllData();
                      combobox.ShowPopup();
                      combobox.EditValue = ID;
                  
                 
              }
          }

          bool IsBind;

          BaseActionService _model;
          public BaseActionService Model
          {
              get
              {
                  return _model;
              }
              set
              {
                  _model = value;
                  BindingAllData();
                  IsBind = false;
              }
          }

          string _SearchString;
          public String SearchString
          {
              get
              {
                  return _SearchString;
              }
              set
              {
                  _SearchString = value;
                  BindingAllData();
                  IsBind = false;

              }
          }

          object data;
        void BindingAllData()
          {
              string[] columns = ColumnDisplay != null ? ColumnDisplay.Split(',') : null;
              if (Model != null)
              {

                  if (String.IsNullOrEmpty(_SearchString))
                  {

                      if (!IsBind)
                      {
                          data = Model.GetList();
                      }
                  }
                  else
                  {
                      data = Model.GetList(_SearchString);

                  }
                  

                  try
                  {
                      
                          combobox.DataBindings.Clear();
                          combobox.DataBindings.Add("EditValue", data, "ID");
                          combobox.Properties.DataSource = data;
                      
                  }
                  catch (Exception ex)
                  {

                      //    throw;
                  }

                 

                  combobox.Properties.DisplayMember = ColumnMain;
                  combobox.Properties.ValueMember = "ID";

                  combobox.Properties.PopulateColumns();
                  combobox.Properties.ShowHeader = true;
 
                  for (int i = 0; i < combobox.Properties.Columns.Count; i++)
                  {
                      {
                          var index = Array.IndexOf(columns, combobox.Properties.Columns[i].FieldName);
                          if (index < 0)
                          {
                              combobox.Properties.Columns[i].Visible = false;
                              combobox.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;
                             
                          }
                       }              
                  }
                  IsBind = true;

               //   combobox.Properties.BestFitMode = BestFitMode.BestFitResizePopup;

                  // Enable auto completion search mode.
                  combobox.Properties.SearchMode = SearchMode.AutoComplete;
                  // Specify the column against which to perform the search.
                  combobox.Properties.AutoSearchColumnIndex = 3; //combobox.Properties.get

              }
             
          }

        private void combobox_Popup(object sender, EventArgs e)
        {
            LoadPopup();
        }
        private void LoadPopup()
        {
            if (!IsBind)
            {
                BindingAllData();

                combobox.ShowPopup();
                //lookUpEdit(sender, e);
            }
        }

        private void combobox_Click(object sender, EventArgs e)
        {
           // BindingAllData();
           // combobox.ShowPopup();
           // combobox.EditValue = ID;
        }


    }
}
